function getQueryParameter(name) {
    const urlParams = new URLSearchParams(window.location.search);
    return urlParams.get(name);
}

document.addEventListener('DOMContentLoaded', () => {
    const articleId = getQueryParameter('articleId');

    if (!articleId) {
        document.querySelector('.article-content').innerHTML = '<p>无法获取文章详情</p>';
        return;
    }

    console.log(`Article ID: ${articleId}`); // 输出 articleId 以进行调试

    // 获取文章详情并显示在页面上
    fetch(`http://localhost:8888/articles/${articleId}`)
        .then(response => response.json())
        .then(data => {
            console.log(data); // 调试输出

            if (data.code === 200) {
                const article = data.data;

                const userInfo = localStorage.getItem('userInfo');
                let account = '';

                if (userInfo) {
                    const user = JSON.parse(userInfo);
                    account = user.account;
                }

                document.querySelector('.article-content').innerHTML = `
                    <h2>${article.title}</h2>
                    <p>${article.content}</p>
                    <div class="article-footer">
                        <span>作者: ${article.account}</span>
                        <span>${new Date(article.createDate).toLocaleDateString()}</span>
                    </div>
                `;

                // 是否显示 编辑/删除 按钮
                if (account === article.account) {
                    const operatingDiv = document.createElement('div');
                    operatingDiv.className = 'operating';

                    const editButton = document.createElement('button');
                    editButton.textContent = '编辑';
                    editButton.onclick = () => {
                        window.location.href = `blog_update.html${location.search}`;
                    };

                    const deleteButton = document.createElement('button');
                    deleteButton.textContent = '删除';
                    deleteButton.onclick = deleteBlog;

                    operatingDiv.appendChild(editButton);
                    operatingDiv.appendChild(deleteButton);

                    document.querySelector('.article-content').appendChild(operatingDiv);
                }
            } else {
                document.querySelector('.article-content').innerHTML = '<p>无法获取文章详情</p>';
            }
        })
        .catch(error => {
            console.error('Error fetching article details:', error);
            document.querySelector('.article-content').innerHTML = '<p>无法获取文章详情</p>';
        });
});

function redirectTo(page) {
    window.location.href = page;
}

function redirectToDetail(id) {
    window.location.href = `blog_detail.html?articleId=${id}`;
}

function deleteBlog() {
    const articleId = getQueryParameter('articleId');

    fetch(`http://localhost:8888/articles/delete/${articleId}`, {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.code === 200) {
            alert('删除成功');
            window.location.href = 'blog_list.html';
        } else {
            alert('删除失败');
        }
    })
    .catch(error => {
        console.error('Error deleting article:', error);
        alert('删除失败');
    });
}
